<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    ES6数值扩展 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						ES6数值扩展
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/09/27 14:27 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#ES6新特性</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h2><a id="number-epsilon" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Number.EPSILON</h2>
<p><code>Number.EPSILON</code>是<code>JavaScript</code>表示的最小精度，它的值接近于<code>2.2204460492503130808472633361816E-16</code>，即\(2^{-52}\)</p>
<ul>
<li>主要用于浮点数运算，对精度进行设置</li>
<li>
<ul>
<li>若两个数的差值小于<code>EPSILON</code>，则认为两个数相等</li>
</ul>
</li>
</ul>
<pre class="line-numbers"><code class="language-js">console.log(0.1 + 0.2); //输出 '0.30000000000000004'
console.log(0.1 + 0.2 === 0.3) //输出 false

function equal(a, b) {
    if (Math.abs(a - b) &lt; Number.EPSILON) //Math.abs() 返回绝对值
        return true;
    else
        return false;
}

console.log(equal(0.1 + 0.2, 0.3)); //输出 true
</code></pre>
<h2><a id="%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%92%8C%E5%85%AB%E8%BF%9B%E5%88%B6" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>二进制和八进制</h2>
<h3><a id="%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9A%84%E8%A1%A8%E7%A4%BA%E6%96%B9%E5%BC%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>二进制的表示方式</h3>
<ul>
<li><code>0b</code>+二进制数值</li>
</ul>
<pre class="line-numbers"><code class="language-js">let binary = 0b1010; //等于十进制 '10'
</code></pre>
<h3><a id="%E5%85%AB%E8%BF%9B%E5%88%B6%E7%9A%84%E8%A1%A8%E7%A4%BA%E6%96%B9%E5%BC%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>八进制的表示方式</h3>
<ul>
<li><code>0o</code>+八进制数值</li>
</ul>
<pre class="line-numbers"><code class="language-js">let octal = 0o12; //等于十进制 '10'
</code></pre>
<h2><a id="number-isfinite" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Number.isFinite()</h2>
<p>检测一个数值是否为有限数</p>
<pre class="line-numbers"><code class="language-js">console.log(Number.isFinite(100)); //输出 true
console.log(Number.isFinite(100 / 0)); //输出 false
console.log(Number.isFinite(Infinity)); //输出 false
</code></pre>
<h2><a id="number-isnan" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Number.isNaN()</h2>
<p>检测一个数值是否为NaN</p>
<pre class="line-numbers"><code class="language-js">console.log(Number.isNaN(123)); //返回 false
</code></pre>
<h2><a id="number-parseint" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Number.parseInt()</h2>
<p>将字符串转换为<code>Int</code></p>
<h2><a id="number-parsefloat" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Number.parseFloat()</h2>
<p>将字符串转换为<code>Float</code></p>
<h2><a id="number-isinteger" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Number.isInteger()</h2>
<p>判断一个数值是否为整数</p>
<ul>
<li>返回<code>boolean</code>类型</li>
</ul>
<h2><a id="math-trunc" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Math.trunc()</h2>
<p>将数字的小数部分抹除</p>
<h2><a id="math-sign" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>Math.sign()</h2>
<p>判断一个数是正数、负数还是零</p>
<ul>
<li>判断为整数返回<code>1</code></li>
<li>判断为零返回<code>0</code></li>
<li>判断为负数返回<code>-1</code></li>
</ul>
<pre class="line-numbers"><code class="language-js">console.log(Math.sign(100)); //输出 '1'
console.log(Math.sign(0)); //输出 '0'
console.log(Math.sign(-100)); //输出 '-1'
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  
<script type="text/javascript"
	src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>















<script src="asset/prism.js"></script>









</body>

</html>